//
//  SelectTimeYearsView.m
//  Doctor
//
//  Created by 阳光 on 16/10/17.
//  Copyright © 2016年 阳光. All rights reserved.
//  年份选择界面

#import "SelectTimeYearsView.h"

static NSString *tableViewCellIdentifier=@"UITableViewCellIdentifier";

@interface SelectTimeYearsView ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *selectTimeYearsTableView;

@property(nonatomic,strong)NSMutableArray *jobFromYearArr;

@end

@implementation SelectTimeYearsView

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
         self.backgroundColor=[[UIColor blackColor] colorWithAlphaComponent:0.5];
        
        _jobFromYearArr=[NSMutableArray array];
        
        for (int i=2016;i>1970;i--)
        {
            [_jobFromYearArr addObject:@(i)];
        }

        
        [self setView];
        
    }
    return self;
}

-(void)setView
{
    UIView *contentView=[[UIView alloc] initWithFrame:CGRectMake(0,(CGRectGetHeight(self.frame)-(CGRectGetWidth(self.frame)-kmargin*2))/2, CGRectGetWidth(self.frame),CGRectGetWidth(self.frame)-kmargin*2)];
    contentView.layer.cornerRadius=kmargin/2;
    contentView.backgroundColor=WhiteColor;
    contentView.backgroundColor=MAIN_BACK_COLOR;
    [self addSubview:contentView];
    
    UILabel *titleLabel=[contentView getLabelWithText:NSLocalizedString(@"Working Year", nil) font:Font_Number(16) textColor:Main_Color textAlignment:NSTextAlignmentCenter lineBreakMode:NSLineBreakByTruncatingTail numberOfLines:1];
    [contentView addSubview:titleLabel];//@"选择从业时间"
    titleLabel.frame=CGRectMake((APPW-200)/2,kmargin,200,30);
    
    
    _selectTimeYearsTableView=[[UITableView alloc] initWithFrame:CGRectMake(0,CGRectGetHeight(titleLabel.frame)+kmargin,CGRectGetWidth(contentView.frame),CGRectGetHeight(contentView.frame)) style:UITableViewStylePlain];
    _selectTimeYearsTableView.dataSource=self;
    _selectTimeYearsTableView.delegate=self;
    _selectTimeYearsTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    _selectTimeYearsTableView.backgroundColor=MAIN_BACK_COLOR;
    _selectTimeYearsTableView.backgroundColor=WhiteColor;
    _selectTimeYearsTableView.showsHorizontalScrollIndicator=NO;
    _selectTimeYearsTableView.showsVerticalScrollIndicator=NO;
     [_selectTimeYearsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:tableViewCellIdentifier];
    [contentView addSubview:_selectTimeYearsTableView];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.jobFromYearArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:tableViewCellIdentifier];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.textLabel.text=[NSString stringWithFormat:@"%@",self.jobFromYearArr[indexPath.row]];
    cell.textLabel.textAlignment=NSTextAlignmentCenter;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    // 获取_guideMainHomeTableView 当前选中的cell
    UITableViewCell *cell=[_selectTimeYearsTableView cellForRowAtIndexPath:indexPath];
    
    if ([self.delegate respondsToSelector:@selector(selectTimeYearWithIndex:timeYear:)])
    {
        [self.delegate selectTimeYearWithIndex:[NSString stringWithFormat:@"%ld",(long)indexPath.row] timeYear:cell.textLabel.text];
    }
    
}

@end
